package com.infowarelab.config.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jradweb.core.controller.JMultiActionController;
import org.jradweb.core.orm.PageResult;
import org.jradweb.core.web.FormBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import com.infowarelab.config.model.Site;
import com.infowarelab.config.service.SiteService;

/**
 * SiteController class .
 * 
 * Created on Mon Jun 30 10:39:47 CST 2008
 * @author Code Generator
 * @version $Id: SiteController.java 156 2008-10-16 09:30:36Z wagzhi $
 */
@Controller
public class SiteController extends JMultiActionController{
	private static final Log log = LogFactory.getLog(SiteController.class);
	private static final String CONTROLLER_NAME= "site";
	
	@Autowired
	@Qualifier("com.infowarelab.config.service.SiteService")
	private SiteService siteService;	 
	
	
	public void setSiteService(SiteService siteService){
		this.siteService= siteService;
	}
	public SiteService getSiteService(){
		return this.siteService;
	}	
	
	@Override
	public String getName() {
		return CONTROLLER_NAME;
	}
	public Map<String, Object> index(HttpServletRequest request,
			HttpServletResponse response) throws Exception {		
		Map<String,Object> map=new HashMap<String,Object>();
		String strPage=request.getParameter("page");
		Integer page=strPage==null?1:Integer.parseInt(strPage); 
		PageResult<Site> pageResult=this.siteService.getPageResult(page, this.getPageSize(),"",null);
		map.put("pageResult", pageResult);
		return map;
	}
	
	public Map<String, Object> show(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String id = (String) request.getParameter("id");

		Site site = this.getSiteService().getSite(Integer.parseInt(id));
		map.put("site",  site );

		return map;

	}
	
	
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String id = (String) request.getParameter("id");

		Site site = this.getSiteService().getSite(Integer.parseInt(id));
	
		this.siteService.delete(site);
		this.getFlash(request).put("message", "message.deleted.success");
		
		return this.redirect("index", map);
	}
	
	public Map<String,Object> create(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		FormBean<Site> bean=(FormBean<Site>)this.getFlash(request).get("bean");
		map.put("bean", bean);
		return map;
	}


	public ModelAndView save(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
	
		Site site = new Site();
		FormBean<Site> bean=this.webDataBinder.bind(site, request);
		if (bean.getHasError()) {
			this.getFlash(request).put("bean", bean);
			this.getFlash(request).put("failMessage", "message.save.failed");
			return this.redirect("create", null);
		}

		this.siteService.save(site);
		this.getFlash(request).put("message",
				"message.saved.success");
		map.put("id", site.getId());
		return this.redirect("show", map);
	}
	
	public Map<String, Object> edit(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String id = (String) request.getParameter("id");
		if(id!=null){
			Site site = this.getSiteService().getSite(Integer.parseInt(id));
			FormBean<Site> bean=this.webDataBinder.wrap(site,request.getLocale());
			map.put("bean", bean);
		}
		else{
			FormBean<Site> bean=(FormBean<Site>)this.getFlash(request).get("bean");
			map.put("bean", bean);
		}
		return map;
	}
	
	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String id = (String) request.getParameter("id");

		Site site = this.getSiteService().getSite(Integer.parseInt(id));
		FormBean<Site> bean=this.webDataBinder.bind(site, request);

		if (bean.getHasError()) {
			this.getFlash(request).put("bean", bean);
			this.getFlash(request).put("failMessage", "message.save.failed");
			return this.redirect("edit", null);
		}
		this.siteService.update(site);

		this.getFlash(request).put("message",
				"message.updated.succuess");
		map.put("id", id);

		return this.redirect("show", map);
	}
	
	
}